package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.LocationChangeEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import screen.ComingSoon;
	import screen.LoadingScreen;
	import screen.MainScreen;
	
	public class London2012MedalBoard extends Sprite
	{
		private var _stageWebView:StageWebView;
		private var myAdvertURL:String = "ad.html";
		
		private const OLYMPIAD_START_DAY:int = 20;
		private const OLYMPIAD_START_MONTH:int = 6;
		
		public function London2012MedalBoard()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP;
			stage.scaleMode = StageScaleMode.SHOW_ALL;
			
			var loader:Loader = new Loader();
			this.addChild( loader );
			var mRequest:URLRequest = new URLRequest("Board.swf");
			
			loader.load( mRequest );
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadLoading);

			this.startAdLoading();

		}
		
		public function startAdLoading():void
		{
			{
				// check that _stageWebView doersn't exist
				if (! _stageWebView) {
					_stageWebView = new StageWebView () ;
					// set the size of the html 'window'
					_stageWebView.viewPort = new Rectangle(0,0, 800, 100);
					// add a listener for when the content of the StageWebView changes
					// THIS LINE DOWN WAS UNCOMMENTED
					//_stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,onLocationChange);
					// start loading the URL;
					_stageWebView.loadURL(myAdvertURL);
				}
				// show the ad by setting it's stage property;
				_stageWebView.stage = stage;
			}
		}
		
		public function toggleAd(event:MouseEvent):void {
			trace("toggling advert",_stageWebView);
			// check that StageWebView instance exists 
			if (_stageWebView) {
				trace("_stageWebView.stage:"+_stageWebView.stage);
				if (_stageWebView.stage == null) {
					//show the ad by setting the stage parameter
					_stageWebView.stage = stage;
				} else {
					// hide the ad by nulling the stage parameter
					_stageWebView.stage = null;
				}
			} else {
				// ad StageWebView doesn't exist - show create it
				
			}
		}
		
		public function destroyAd(event:MouseEvent):void {
			// check that the instace of StageWebView exists
			if (_stageWebView) {
				trace("removing advert");
				// destroys the ad
				_stageWebView.stage = null;
				_stageWebView = null;
			}
		}
		
		public function onLocationChange(event:LocationChangeEvent):void {
			// check that it's not our ad URL loading
			if (_stageWebView.location != myAdvertURL) {
				// destroy the ad as the user has kindly clicked on my ad
				destroyAd(null);
				// Launch a normal browser window with the captured  URL;
				navigateToURL( new URLRequest( event.location ) );
			}
		}
		// setup button listeners
		
		public function loadComingSoon():void
		{	
			var comingSoon:ComingSoon = new ComingSoon(this);
		}
		
		public function loadLoading( event:Event ):void
		{	
			var dateNow:Date = new Date();
			trace(dateNow);
			
			if((dateNow.date < OLYMPIAD_START_DAY) && (dateNow.month == OLYMPIAD_START_MONTH))
			{
				this.loadComingSoon();
			} else {
				var loadingScreen:LoadingScreen = new LoadingScreen(this);
				this.loadBoard();
			}
			
		}
		
		public function loadBoard():void
		{
			var mainScreen:MainScreen = new MainScreen(this);
		}
	}
}